package com.security.door.controller.cardManage;

import com.github.pagehelper.PageInfo;
import com.security.door.core.wrapper.WrapMapper;
import com.security.door.core.wrapper.Wrapper;
import com.security.door.model.domain.csCardinfo;
import com.security.door.model.dto.csCardinfoDto;
import com.security.door.model.vo.csCardinfoVo;
import com.security.door.service.IcsCardinfoService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.MediaType;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;
import java.util.Date;
import java.util.UUID;

@RestController
@Api(value = "WEB - csCardinfoController", produces = MediaType.APPLICATION_JSON_UTF8_VALUE)
public class csCardinfoController {
    @Autowired
    HttpSession session;
    @Autowired
    HttpServletRequest httpServletRequest;
    @Autowired
    IcsCardinfoService icsCardinfoService;

    @GetMapping("/csCardinfo/queryListWithPage")
    @ApiOperation(value = "分页查询所有信息", notes = "分页查询所有信息")
    public Wrapper<PageInfo<csCardinfoVo>> queryListWithPage() {
        csCardinfoDto entityDto = new csCardinfoDto();
        int pageIndex = httpServletRequest.getParameter("start") == null ? 0 : Integer.parseInt(httpServletRequest.getParameter("start"));
        int pageSize = httpServletRequest.getParameter("length") == null ? Integer.MAX_VALUE : Integer.parseInt(httpServletRequest.getParameter("length"));
        String searchStr = httpServletRequest.getParameter("search[value]");
        entityDto.setKeyWord(searchStr);
        entityDto.setPageIndex(pageIndex / pageSize + 1);
        entityDto.setPageSize(pageSize);
        var list = icsCardinfoService.queryListWithPage(entityDto);
        return WrapMapper.ok(list);
    }

    @PostMapping("/csCardinfo/Add")
    @ApiOperation(value = "分页查询所有信息", notes = "分页查询所有信息")
    public Wrapper Add(@RequestBody csCardinfo entity) {
        entity.setCreatetime(new Date());
        if (null == entity.getGuid() || "".equals(entity.getGuid())) {
            boolean result = icsCardinfoService.checkCardNo(entity);
            if (result)
                return WrapMapper.error("凭证编号已经存在");
            entity.setGuid(UUID.randomUUID().toString());
            icsCardinfoService.save(entity);
        } else {
            icsCardinfoService.update(entity);
        }
        return WrapMapper.ok("操作成功");
    }

    @GetMapping("/csCardinfo/deleteMore/{guids}")
    @ApiOperation(value = "批量删除", notes = "批量删除")
    public Wrapper deleteByIds(@PathVariable("guids") String guids) {
        icsCardinfoService.deleteByIds(guids);
        return WrapMapper.ok("操作成功");
    }

    @GetMapping("/csCardinfo/selectNotIssueCars")
    @ApiOperation(value = "查询未发行卡", notes = "查询未发行卡")
    public Wrapper selectNotIssueCars() {
        var list = icsCardinfoService.selectNotIssueCars();
        return WrapMapper.ok(list);
    }
}
